

//渠道
ascription2()
function ascription2() {
    $.ajax({
        type: "post",
        url: root.dockingplatform.allChannelConfig,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {

        },
        success: function (data) {
            console.log(data);
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#ascription_lsit").html(interText(dataInter));
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });


}
    $('#addBtn').click(function () {
        sessionStorage.removeItem("id");
        var index = layer.open({
            type: 2,
            title: '新增询价单',
            content: 'addInquiry.html',
            end:function () {
                location.reload();
            }
        });
        layer.full(index);
    });

    //获取操作人的姓名及ID
    var loginData=JSON.parse(web.readCookie("loginData"));
    var service_name = loginData.user.name;
    var service_id = loginData.user.user_ID;
   // 重置功能
    $('#resetBtn').click(function () {
        $('#inquiry_status').val('');
        //$("#submitBtn").trigger('clicsubmitBtnk');
    });
    //从二级页面返回列表页面刷新
    function doSubmit(){
        $('#submitBtn').click();

    }
    // 查询功能
    $("#form-search").Validform({
        tiptype:2,
        callback:function(form){
            /*if($("#ascription_lsit option:selected").val()==null || $("#ascription_lsit option:selected").val()==undefined || $("#ascription_lsit option:selected").val()==''){
                layer.msg('请选择渠道！', {icon: 5, time: 1000});
                return false;
            }*/

            if($('#timemin').val()!=''&&$('#timemax').val()==''){
                layer.msg('结束时间不能为空！', {icon: 5,time:1000});
                return false;
            }
            if($('#timemin').val()==''&&$('#timemax').val()!=''){
                layer.msg('开始时间不能为空！', {icon: 5,time:1000});
                return false;
            }
            sessionStorage.setItem("customerName",$('#ascription_lsit option:selected').text());
            getList(1,'20')    // 默认倒序
            return false;
        }
    });
    //通过询价时间排序   并查询   向上倒序   向下正序
    var flag1=false;
    $('#sortByCreatedTime img').click(function(){
         console.log(flag1);
        if(flag1){
            $(this).attr('src','../../img/copyup.png');
            getList(1,"20");
        }
        else{
            $(this).attr('src','../../img/downorder.png');
            getList(1,"10");
        }
        flag1=!flag1;
     });
     // 通过询价时间排序   并查询
    var flag2=true;
     $('#sortByAllNumber img').click(function () {
        /* if($(this).text()=='↑'){
             $(this).text('↓');
             getList(1,"30");
         }else{
             $(this).text('↑');
             getList(1,"40");
         }*/
        console.log(this);
        if(flag2){
            $(this).attr('src','../../img/copyup.png');
            getList(1,"40");
        }
        else{
            $(this).attr('src','../../img/downorder.png');
            getList(1,"30");
        }
        flag2=!flag2;
     });
    //询价单状态下拉框
    $('#inquiryStat1').val('');
    $('#inquiryStat1').change(function () {
        var value1 = $(this).val();
        var value2=$('#inquiryStat2 option:selected') .val();
        if(value1=="1020"){
            $('#inquiryStat2').show().attr({'inquiry_status1':value1});
            if(value2=='00'){
                $('#inquiry_status').val(value1);
            }else if(value2 == '01'){
                $('#inquiry_status').val('10');
            }else{
                $('#inquiry_status').val('20');
            }
        }else if(value1=="3040"){
            $('#inquiryStat2').show().attr({'inquiry_status1':value1});
            if(value2=='00'){
                $('#inquiry_status').val(value1);
            }else if(value2 == '01'){
                $('#inquiry_status').val('30');
            }else{
                $('#inquiry_status').val('40');
            }
        }else{
            $('#inquiry_status').val(value1);
            $('#inquiryStat2').hide().attr({'inquiry_status1':''});
        }
    });
    $('#inquiryStat2').change(function () {
        var inquiryStat1 = $(this).attr('inquiry_status1');
        var value2 = $(this).val();
        if(inquiryStat1=='1020'){
            if(value2=='00'){
                $('#inquiry_status').val('1020');
            }else if(value2=='01'){
                $('#inquiry_status').val('10');
            }else{
                $('#inquiry_status').val('20');
            }
        }else if(inquiryStat1=='3040'){
            if(value2=='00'){
                $('#inquiry_status').val('3040');
            }else if(value2=='01'){
                $('#inquiry_status').val('30');
            }else{
                $('#inquiry_status').val('40');
            }
        }
    });
    //地址
    discountdata(123);
    // 获取 页面搜索条件

//表格相关操作
$('#datatable').on("click",".inquiryOperate",function () {
    var rowData = $(this).attr('rowData').split(',');
    var inquiry_status = rowData[12];
    var inquiry_id = rowData[0];
    var back;
    var matcher_id = '', matching_name = '', fix_price_id = '', fix_price_name = '';
    var customer_id = rowData[17];
    var customer_name = rowData[2];
    var receiver_province = rowData[18]; //cityCode
    var platform = rowData[19];
    console.log(rowData);
    if(inquiry_status == '50'){   //查看
        // location.href="productDetailOffer.html?inquiry_id="+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name;
        goNextPage('详情页信息','productDetailOffer.html?inquiry_id='+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name+'&customer_id='+customer_id+'&customer_name='+customer_name+'&receiver_province='+receiver_province+'&platform='+platform)
        return false;
    }else if(inquiry_status == '60'||inquiry_status == '70') {
        goNextPage('退回信息','productDetail.html?inquiry_id='+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name+'&customer_id='+customer_id+'&customer_name='+customer_name+'&receiver_province='+receiver_province+'&platform='+platform)
        // location.href = "productDetail.html?inquiry_id="+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name;
        return false;
    }else if(inquiry_status == '10'){
        back = '1';
        matcher_id = service_id;
        matching_name = service_name;
    }else if (inquiry_status == '20'){
        back = '0';
        matcher_id = service_id;
        matching_name = service_name;
    }else if(inquiry_status == '30'){
        back = '1';
        matcher_id = '';
        matching_name = '';
        fix_price_id = service_id;
        fix_price_name = service_name;
    } else if (inquiry_status == '40'){
        back = '0';
        matcher_id = '';
        matching_name = '';
        fix_price_id = service_id;
        fix_price_name = service_name;
    }
    var operParams = {
        "async": true,
        "crossDomain": true,
        "url": root.dockingplatform.InquiryOrderMatch,
        "method": "POST",
        "headers": {
            "Content-Type": "application/json"
        },
        "processData": false,
        "data": JSON.stringify({
            "inquiry_id":inquiry_id ,
            "back": back,
            "state": inquiry_status,
            "matcher_id": matcher_id,
            "matching_name": matching_name,
            "fix_price_id":fix_price_id,
            "fix_price_name":fix_price_name
        })
    };
    $.ajax(operParams).done(function (response) {
        if (response.code == '0') {
            if (inquiry_status == '10' || inquiry_status == '20') {
                // location.href="match.html?inquiry_id="+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name;
                goNextPage('待匹配','match.html?inquiry_id='+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name+'&customer_id='+customer_id+'&customer_name='+customer_name+'&receiver_province='+receiver_province+'&platform='+platform)
            } else if (inquiry_status == '30' || inquiry_status == '40') {
                goNextPage('待定价','fixPrice.html?inquiry_id='+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name+'&customer_id='+customer_id+'&customer_name='+customer_name+'&receiver_province='+receiver_province+'&platform='+platform)
                // location.href="fixPrice.html?inquiry_id="+inquiry_id+'&inquiry_status='+inquiry_status+'&matcher_id='+matcher_id+'&matching_name='+matching_name+'&fix_price_id='+fix_price_id+'&fix_price_name='+fix_price_name;
            } else {

            }
        } else if (response.code == '60' || response.code == '70') {
            layer.msg('该询价单已失效，请刷新页面！', {icon: 5,time:1000});
        } else if(response.code == '10'||response.code == '30'){
            layer.msg('该询价单状态已经改变，请重新查询！', {icon: 5,time:1000});
        }  else { // 10或者20
            layer.msg('该询价单被锁定，您不能进行操作，处理人为'+response.name, {icon: 5,time:1000});
        }
    });
});
function goNextPage(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}


//点搜索之后查询数据列表
/*表格渲染*/
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function orderList(data) {
    dataSource.splice(0, dataSource.length);//清空数组
    if (data.data!=null && data.data!=undefined && data.data!=''){
        $.each(data.data.list, function (i, field) {
            console.log(field);
            var rowData = new Array();
            rowData.push(field.inquiry_id);
            rowData.push(field.platform);
            if(field.customer_name){
                rowData.push(field.customer_name);
            }else{
                rowData.push(field.login_account);
            }
            rowData.push(field.linkman);
            rowData.push(field.receiver_province_name);
            rowData.push(field.created_time);
            rowData.push(field.all_number);
            rowData.push(field.exist_price_number);
            rowData.push(field.matching_name);
            rowData.push(field.matching_time);
            rowData.push(field.fix_price_name);
            rowData.push(field.fix_price_time);
            rowData.push(field.inquiry_status);
            rowData.push(field.inquiry_status);
            dataSource.push(rowData);
            rowData.push(field.matcher_id);
            rowData.push(field.fix_price_id);
            rowData.push(field.id);
            rowData.push(field.customer_id);
            rowData.push(field.receiver_city);     //仓库id -->城市code
            rowData.push(field.platform);               //平台普通 签约
        });
    } /*else {
        $.each(data, function (i, field) {
            console.log(field);
            var rowData = new Array();
            rowData.push(field.inquiry_id);
            rowData.push(field.platform);
            if(field.customer_name){
                rowData.push(field.customer_name);
            }else{
                rowData.push(field.login_account);
            }
            rowData.push(field.linkman);
            rowData.push(field.receiver_province_name);
            rowData.push(field.created_time);
            rowData.push(field.all_number);
            rowData.push(field.exist_price_number);
            rowData.push(field.matching_name);
            rowData.push(field.matching_time);
            rowData.push(field.fix_price_name);
            rowData.push(field.fix_price_time);
            rowData.push(field.inquiry_status);
            rowData.push(field.inquiry_status);
            dataSource.push(rowData);
            rowData.push(field.matcher_id);
            rowData.push(field.fix_price_id);
            rowData.push(field.id);
            rowData.push(field.customer_id);
            rowData.push(field.receiver_city);     //仓库id -->城市code
            rowData.push(field.platform);               //平台普通 签约
        });
    }*/


    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            //"order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render":function (data,type,row,meta) {
                        var value = sessionStorage.getItem("customerName");
                        /*if(row[1]=="10"){
                            value='签约平台';
                        }else {
                            value='普通平台'
                        }
                        */
                        return value;
                    },
                    "targets": 1
                },
                {
                    "render":function (data,type,row,meta) {
                        var value;
                        if(row[9]=="1000-01-01 00:00:00"){
                            value='';
                        }else {
                            value=row[9]
                        }
                        return value;
                    },
                    "targets": 9
                },
                {
                    "render":function (data,type,row,meta) {
                        var value;
                        if(row[11]=="1000-01-01 00:00:00"){
                            value='';
                        }else {
                            value=row[11];
                        }
                        return value;
                    },
                    "targets": 11
                },
                {
                    "render": function(data,type,row,meta){
                        state=row[12];
                        var inquiryType='';
                        if(state == '10'){
                            inquiryType= '待匹配';
                            //operation = '匹配';
                         }else if(state == '20'){
                            inquiryType= '待匹配';
                            //operation = '匹配';
                        }else if(state == '30'){
                            inquiryType= '待定价';
                            //operation = '定价';
                        }else if(state == '40'){
                            inquiryType= '待定价';
                            //operation = '定价';
                        }else if(state == '50'){
                             inquiryType= '已报价';
                             //operation = '查看';
                         }else if(state == '60'){
                            inquiryType= '已退回';
                            //operation = '查看';
                         }else if(state == '70'){
                             inquiryType = '已撤销';
                             //operation = '查看';
                        }

              var val= '<span inquiry_status="'+state+'">'+inquiryType+'</span>';
              return val;
                    },
                    "targets": 12
                },
                {
                    "render": function(data,type,row,meta){
                        var state = row[13];
                        var operation='';
                        if(state == '10'){
                            // inquiryType= '待匹配';
                            operation = '匹配';
                         }else if(state == '20'){
                           //  inquiryType= '待匹配';
                            operation = '匹配';
                        }else if(state == '30'){
                            // inquiryType= '待定价';
                            operation = '定价';
                        }else if(state == '40'){
                            // inquiryType= '待定价';
                            operation = '定价';
                        }else if(state == '50'){
                            // inquiryType= '已报价';
                            operation = '查看';
                        }else if(state == '60'){
                            // inquiryType= '已退回';
                            operation = '查看';
                        }else if(state == '70'){
                            // inquiryType = '已撤销';
                            operation = '查看';
                        }
                        var val= '<a href="javascript:void(0);" inquiry_status="'+row[13]+'" rowData="'+row+'" class="inquiryOperate powerButton 616">'+operation+'</a>';
                        return val;
                    },
                    "targets": 13
                }
            ]
        });
        //权限控制函数
        power()
    };
}




function getList(page_num,orderNum,aa){
     //url ="http://localhost:7130/openPlatform/selectInquiryBill";
     url = root.dockingplatform.selectInquiryBill;
    var dataDict=getParams(orderNum,aa);
    var customer_id="";
    if($("#ascription_lsit option:selected").val()==""||$("#ascription_lsit option:selected").val()==undefined|| $("#ascription_lsit option:selected").val()==null){
        customer_id=""
    }else{
        customer_id=$("#ascription_lsit option:selected").val();
    }
    $.ajax({
        contentType: "application/json",
        type: "POST",
        url: url,
        cache: false,  //禁用缓存
        dataType: "json",
        data: JSON.stringify({
            "total":"",
            "totalPage":"",
            "pageNumber":page_num,
            "pageSize":10,
            "startIndex":"",
            "inquiry_id":dataDict["inquiry_id"],
            "customer_name":dataDict["customer_name"],
            "inquiry_status":dataDict["inquiry_status"],
            "start_time":dataDict["start_time"],
            "end_time":dataDict["end_time"],
            "matching_name":dataDict["matching_name"],
            "fix_price_name":dataDict["fix_price_name"],
            "receiver_province":dataDict["receiver_province"],
            "linkman":dataDict["linkman"],
            "created_time":dataDict["created_time"],
            "all_number":dataDict["all_number"],
            "platform":dataDict["platform"],
            "inquiry_name":dataDict["project_name"],
            "product_name":dataDict["product_name"],
            "channel_id":customer_id
        }),
        success: function (result) {
            sessionStorage.setItem("Instatus1",'');
            sessionStorage.setItem("Instatus2",'');
            console.log(result);
            orderList(result);
            $('.M-box3').pagination({
                pageCount: result.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api){
                    $.ajax({
                        contentType: "application/json",
                        type: "POST",
                        url: url,
                        cache: false,  //禁用缓存
                        dataType: "json",
                        data: JSON.stringify({
                            pageNumber: api.getCurrent(), //当前页码
                            pageSize: 10,//每页显示条数
                            startIndex:"",
                            inquiry_id:dataDict["inquiry_id"],
                            customer_name:dataDict["customer_name"],
                            inquiry_status:dataDict["inquiry_status"],
                            start_time:dataDict["start_time"],
                            end_time:dataDict["end_time"],
                            matching_name:dataDict["matching_name"],
                            fix_price_name:dataDict["fix_price_name"],
                            receiver_province:dataDict["receiver_province"],
                            linkman:dataDict["linkman"],
                            created_time:dataDict["created_time"],
                            all_number:dataDict["all_number"],
                            platform:dataDict["platform"],
                            inquiry_name:dataDict["project_name"],
                            product_name:dataDict["product_name"],
                            channel_id:customer_id
                        }),
                        success: function (result) {
                            orderList(result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}
function discountdata (val) {
    if(val==123){
        //初始化 省
        var user_data = {
            level: '101',
            // memberId:'10258',
        }
        var succCallBack = function (data, status, response) {
            if(data.errorCode=="1"){
                var tab_data=data.list;
                if(tab_data==''){
                    cer+='<option value=""></option>'
                    $("#getArea").html(cer);
                }else{
                    var cer="<option value=''>请选择收货区域</option>"
                    for(var i=0; i<tab_data.length; i++){
                        cer+='<option value="'+tab_data[i].address_id+'">'+tab_data[i].name+'</option>'
                    }
                    $("#getArea").html(cer);
                }
            }
        };
        web.doAjax(root.orderInter.order_return_address,'post',user_data, succCallBack);
    }else if(val==1){
        //市
        var special=$('#getArea').val();
        if(special==""){
            var cer="<option value=''>请选择收货位置</option>"
            $("#getPlace").html(cer);
        }else {
            var user_data = {
                level: '102',
                // memberId:'10258',
                addressIdStr:special
            }
            var succCallBack = function (data, status, response) {
                if(data.errorCode=="1"){
                    var tab_data=data.list;
                    if(tab_data==''){
                        cer+='<option value=""></option>';
                        $("#getPlace").html(cer);
                    }else{
                        var cer="<option value=''>请选择收货位置</option>";
                        for(var i=0; i<tab_data.length; i++){
                            cer+='<option value="'+tab_data[i].address_id+'">'+tab_data[i].name+'</option>'
                        }
                        $("#getPlace").html(cer);
                    }
                }
            };
            web.doAjax(root.orderInter.order_return_address,'post',user_data, succCallBack);
        }
    }
}
function getParams(orderNum,aa){
    var created_time='';
    var all_number='';
    if(orderNum=='10'||orderNum=='20'){
        created_time=orderNum;
        all_number='';
    }else if(orderNum=='30'||orderNum=='40'){
        all_number = orderNum-20;
        created_time='';
    }else{
        all_number='';
        created_time='';
    }
    var created_time = created_time;
    var inquiry_id = $('#inquiry_id').val();
    var customer_name  = $('#customer_name').val();
    var inquiry_status = "";
    if(aa=='1000'){
        inquiry_status = "1020"
    }else if(aa=='2000'){
        inquiry_status = "3040"
    }else {
        inquiry_status =  $('#inquiry_status').val()
    }
    var start_time = $('#timemin').val();
    var end_time = $('#timemax').val();
    var matching_name = $('#matching_name').val();
    var fix_price_name = $('#fix_price_name').val();
    var receiver_province = $('#getArea').val();
    var linkman = $('#linkman').val();
    var all_number = all_number;
    var platform = $('#platform').find("option:selected").val();
    var project_name = $('#project_name').val();
    var product_name = $('#product_name').val();
    return {
        "inquiry_id":inquiry_id,
        "customer_name":customer_name,
        "inquiry_status":inquiry_status,
        "start_time":start_time,
        "end_time":end_time,
        "matching_name":matching_name,
        "fix_price_name":fix_price_name,
        "receiver_province":receiver_province,
        "linkman":linkman,
        "created_time":created_time,
        "all_number":all_number,
        "platform":platform,
        "inquiry_name":project_name,
        "product_name":product_name,
        warehouseIds:sessionStorage.getItem("regions"),//仓库权限串
        belongGroupIds:sessionStorage.getItem("companys"),//销售归属权限串
        salesManagerIds:sessionStorage.getItem("personnels"),//销售经理权限串
    }
}

//弹框跳转
var Instatus1= sessionStorage.getItem("Instatus1");//待匹配
var Instatus2= sessionStorage.getItem("Instatus2");//待定价
if(Instatus1==1020){
    $('#inquiryStat1').val(Instatus1);
    getList(1,20,1000)
}
if(Instatus2==3040){
    $('#inquiryStat1').val(Instatus2);
    getList(1,20,2000)
}

